﻿using Microsoft.AspNetCore.Mvc;
using star_questionnair.Models;
using star_questionnair.Services;

namespace star_questionnair.Controllers;

/// <summary>
    /// 控制器：处理选项相关的请求
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class OptionController : ControllerBase
    {
        private readonly IOptionService _optionService;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        public OptionController(IOptionService optionService)
        {
            _optionService = optionService;
        }

        /// <summary>
        /// 新增选项
        /// </summary>
        /// <param name="option">选项对象</param>
        /// <returns>操作结果</returns>
        [HttpPost("add")]
        public IActionResult Add([FromBody] Option option)
        {
            _optionService.Save(option);
            return Ok(new { message = "success" });
        }

        /// <summary>
        /// 根据ID删除选项
        /// </summary>
        /// <param name="id">选项ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("delete/{id}")]
        public IActionResult DeleteById(int id)
        {
            _optionService.RemoveById(id);
            return Ok(new { message = "success" });
        }

        /// <summary>
        /// 批量删除选项
        /// </summary>
        /// <param name="ids">选项ID列表</param>
        /// <returns>操作结果</returns>
        [HttpDelete("delete/batch")]
        public IActionResult DeleteBatch([FromBody] List<int> ids)
        {
            _optionService.RemoveByIds(ids);
            return Ok(new { message = "success" });
        }

        /// <summary>
        /// 更新选项
        /// </summary>
        /// <param name="option">选项对象</param>
        /// <returns>操作结果</returns>
        [HttpPut("update")]
        public IActionResult Update([FromBody] Option option)
        {
            _optionService.UpdateById(option);
            return Ok(new { message = "success" });
        }

        /// <summary>
        /// 根据ID查询选项
        /// </summary>
        /// <param name="id">选项ID</param>
        /// <returns>选项详情</returns>
        [HttpGet("selectById/{id}")]
        public IActionResult SelectById(int id)
        {
            var option = _optionService.GetById(id);
            if (option == null)
            {
                return NotFound(new { message = "Option not found" });
            }
            return Ok(option);
        }

        /// <summary>
        /// 查询所有选项（可根据属性过滤）
        /// </summary>
        /// <param name="option">过滤条件</param>
        /// <returns>选项列表</returns>
        [HttpPost("selectAll")]
        public IActionResult SelectAll([FromBody] Option option)
        {
            var options = _optionService.SelectAll(option);
            return Ok(options);
        }
    }